<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Login extends Controller
{
    public function login()
    {

        $username = trim(input('param.account'));
        $password = input('param.password');

        if ($username == '') {
            exit(json_encode(['code' => 1, 'msg' => 'error']));
        } elseif ($password == '') {
            exit(json_encode(['code' => 1, 'message' => 'error',]));
        }

        // 验证用户
        $this->db = new SysDb();
        $admin = $this->db->table('tenant_account')->where(['account' => $username])->item();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "账号错误"]));
        }
        if (md5($password) != $admin['password']) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "密码错误"]));
        }
        if ($admin['status'] == -1) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "用户被冻结"]));
        }
        if ($admin['status'] == 1) {
            if ($admin['role_id'] != 1) {
                $list = Db::table('tenant_role_menu')
                    ->alias('trm')
                    ->join('tenant_menu tm', 'trm.menu_id = tm.id', "left")
                    ->where(['role_id' => $admin['role_id']])
                    // ->field(array(' ai.id' => 'aiid', 'ai.create_time' => 'aicreate_time', 'ai.status' => 'aistatus', 'ai.bus_tail_id_str' => 'aibus_tail_id_str', 'ai.remark' => 'airemark', 'ai.account' => 'aiaccount', 'ar.role_name' => 'arrole_name', 'ai.login_time' => 'ailogin_time', 'ai.is_store_manager' => 'aiis_store_manager', 'ai.store_id' => 'aistore_id', 'ai.role_id' => 'airole_id'))
                    ->select();
                $listlent = Db::table('tenant_role_menu')
                    ->alias('trm')
                    ->join('tenant_menu tm', 'trm.menu_id = tm.id', "left")
                    ->where(['role_id' => $admin['role_id']])
                    ->count();
                // $list = $this->db->table('tenant_role_menu')->where(['role_id' => $admin['role_id']])->lists();
                // $listlent = $this->db->table('tenant_role_menu')->where(['role_id' => $admin['role_id']])->count();
            } else {
                $list = $this->db->table('tenant_menu')->where(['flag' => 1])->lists();
                $listlent = $this->db->table('tenant_menu')->where(['flag' => 1])->count();
            }
            $lists = array();
            // array_push($lists,$list);
            // array_push($lists,$admin['is_store_manager']);
            $lists['listlent'] = $listlent;
            $lists['menuList'] = $list;
            $lists['tenantid'] = $admin['tenant_id'];
            $lists['isStoreManager'] = 0;
            $lists['token'] = "Bearer_eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE2MzI0NzIwOTEsInN1YiI6IjE4MDMwMzg3MDg4Uk9MRV9CVVMiLCJjcmVhdGVkIjoxNjMyMjk5MjkxMjQyfQ.rrykxo10lVJWTjLfhBhAw3sgl9OqNxNpSayA9CojLVnuAPo5JoCbAZOeDIcCUNHLEoxR9JdKW9SMHHZRTbulDQ";
            exit(json_encode(['message' => 'succ', 'result' => $lists]));
        }
        // if ($admin['status'] == 0) {
        //     exit(json_encode(['message' => 'succ']));
        // }

    }
}
